package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Patient;
import com.ruoyi.system.mapper.PatientMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.PatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    private static final Logger log = LoggerFactory.getLogger(PatientServiceImpl.class);

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询患者主数据
     *
     * @param id 患者主数据主键
     * @return 患者主数据
     */
    @Override
    public Patient selectPatientById(Long id)
    {
        return patientMapper.selectPatientById(id);
    }

    /**
     * 查询患者主数据列表
     *
     * @param patient 患者主数据
     * @return 患者主数据
     */
    @Override
    public List<Patient> selectPatientList(Patient patient)
    {
        return patientMapper.selectPatientList(patient);

    }

    /**
     * 新增患者主数据
     *
     * @param patient 患者主数据
     * @return 结果
     */
    @Override
    public int insertPatient(Patient patient)
    {
        patient.setCreateTime(DateUtils.getNowDate());
        return patientMapper.insertPatient(patient);
    }

    /**
     * 修改患者主数据
     *
     * @param patient 患者主数据
     * @return 结果
     */
    @Override
    public int updatePatient(Patient patient)
    {
        patient.setUpdateTime(DateUtils.getNowDate());
        return patientMapper.updatePatient(patient);
    }

    /**
     * 批量删除患者主数据
     *
     * @param ids 需要删除的患者主数据主键
     * @return 结果
     */
    @Override
    public int deletePatientByIds(Long[] ids)
    {
        return patientMapper.deletePatientByIds(ids);
    }

    /**
     * 删除患者主数据信息
     *
     * @param id 患者主数据主键
     * @return 结果
     */
    @Override
    public int deletePatientById(Long id)
    {
        return patientMapper.deletePatientById(id);
    }

    @Override
    public List<Patient> selectPatientListBySjdwNameAndSjys(Patient patient,SysUser sysUser) {
        Long deptId = sysUser.getDeptId();
        /**
         * 注册用户进入患者列表，通过注册用户的当前所在单位（后台管理分配的单位），查询出当前所在单位一致的患者列表。
         */
        return patientMapper.selectPatientListBySjdwNameAndSjys(deptId,patient.getPatientName());
    }

    @Override
    public String savePatient(List<Patient> patientList) {

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        patientList.stream().forEach(patient -> {
            String sjdwName = patient.getSjdwName();
            String patienPhone = patient.getPatientPhone();
            String patientName = patient.getPatientName();
            Long outId = patient.getId();
            try {
                //根据外部id判断是否存在患者，如果存在，则更新，否则修改
                if(outId != null){
                    Patient patient1 = patientMapper.getPatientByOutId(outId);
                    if(patient1 != null){ //不为空，更新
                        Long id = patient1.getId();
                        patient.setId(id);
                        patientMapper.updatePatient(patient);
                    }else{
                        patient.setId(null);
                        patient.setOutId(outId);
                        patientMapper.insertPatient(patient);
                    }
                }
            }catch (Exception e){
                String msg = "<br/> 送检单位：" + sjdwName + "、受检人 " + patientName + " ，电话：" + patienPhone +"传输失败";
                failureMsg.append(msg + e.getMessage());
                log.error(msg + e.getMessage());
            }
        });
        if(failureMsg.toString().length() > 0){
            return failureMsg.toString();
        }else {
            successMsg.append("数据传输成功");
            return successMsg.toString();
        }
    }
}
